<?php	
class member {
	// Database variables
	var $username;
	var $password;
	var $memberName;
	var $teamName;
	var $moneyPaid;
	var $active;
	var $miniPop;
	var $playerCode = array();
	var $playerWeekIn = array();
	var $playerWeekOut = array();
	var $totalValue;
	var $currTotalPoints;		// Points the current team scored
	var $prevTotalPoints;		// Points that subs scores
	var $numGK;
	var $numDEF;
	var $numMID;
	var $numATT;
	var $scorePerWeekArray = array();
	var $positionPerWeekArray = array();
	var $matchingScorePerWeekArray = array();
	var $maxWeeks;
	var $captain = array();
	var $captainsEnabled;


	function member() 
	{
		$this->totalScore = 0;
		$this->prevTotalPoints = 0;
		$this->currTotalPoints = 0;
		$this->numGK  = 0;
		$this->numDEF = 0;
		$this->numMID = 0;
		$this->numATT = 0;
		$this->totalValue = 0;
		
		$cDatabase = new database();

		$connect = $cDatabase->connectToDatabase();
		$activeDatabase = $cDatabase->getActiveDatabase($connect);
	
		$db = $cDatabase->selectDatabase($connect, $activeDatabase);

		$this->maxWeeks = $cDatabase->GetMaxWeeks($connect);
		$this->captainsEnabled = $cDatabase->GetCaptainsEnabled($connect);
		
		for ($week = 1; $week <= $this->maxWeeks; $week++)
		{
			$this->captain[$week] = 0;	// i.e. no captain
		}
	}
	
	function getUsername()
	{
		return $this->username;
	}
	function setUsername($temp)
	{
		if ($temp != NULL)
			$this->username = $temp;
		else
			return 1;
		return 0;
	}
	
	function getPassword()
	{
		return $this->password;
	}
	function setPassword($temp)
	{
		if ($temp != NULL)
			$this->password = $temp;
		else
			return 1;
		return 0;
	}
	
	function setMemberName($temp)
	{
		if ($temp != NULL)
		{
			$this->memberName = $temp;

			// If this member has an underscore then we need to replace it with an apostrophe
			substr_replace("_", "'", 0);
		}
		else
		{
			return 1;
		}
		return 0;
	}
	function getMemberName()
	{
		return $this->memberName;
	}
	
	function setTeamName($temp)
	{
		if ($temp != NULL)
			$this->teamName = $temp;
		else
			return 1;
		return 0;
	}
	function getTeamName()
	{
		return $this->teamName;
	}
	
	function setMoneyPaid($temp)
	{
		if ($temp != NULL)
			$this->moneyPaid = $temp;
		else
			return 1;
		return 0;
	}
	function getMoneyPaid()
	{
		return $this->moneyPaid;
	}
	
	function setActive($temp)
	{
		if ($temp != NULL)
			$this->active = $temp;
		else
			return 1;
		return 0;
	}
	function getActive()
	{
		return $this->active;
	}
	
	function setMinipop($temp)
	{
		if ($temp != NULL)
			$this->miniPop = $temp;
		else
			return 1;
		return 0;
	}
	function getMinipop()
	{
		return $this->miniPop;
	}
	
	function addPlayerValue($temp)
	{	// Floating point arithmetic
		$this->totalValue = bcadd($this->totalValue, $temp, 1);  
	}
	function subtractPlayerValue($temp)
	{	// Floating point arithmetic
		$temp = -$temp;
		$this->totalValue = bcadd($this->totalValue, $temp, 1);  
	}
	function getTeamValue()
	{
		return $this->totalValue;
	}
	
	function addPlayerScore($temp, $flag)
	{
		if ($flag == 'CURR')
			$this->currTotalPoints += $temp;
		else if ($flag == 'PREV')
			$this->prevTotalPoints += $temp;
		else
			return 1;
		return 0;
	}
	function getTotalScore($flag)
	{
		if ($flag == 'CURR')
			return $this->currTotalPoints;
		else if ($flag == 'PREV')
			return $this->prevTotalPoints;
		else if ($flag == 'TOTAL')
			return ($this->prevTotalPoints + $this->currTotalPoints);
		else
			return 'ERROR';
	}
	
	function getMoneyOwed($numberTransfers)
	{
		return ($this->moneyPaid - ENTRYFEE - (TRANSFERFEE * $numberTransfers) );
	}
  
	function getCaptainForWeek($week)
	{
		return $this->captain[$week];
	}
	
	function loadCaptainArray($row)
	{
		for ($i = 1; $i <= $this->maxWeeks; $i++)
		{
			$captainVariable = getCaptainVariable($i);
			if ($row[$captainVariable] != NULL)		// Is this a valid captain
			{
				$this->captain[$i] = $row[$captainVariable];
			}
		}
	}
	
	function loadMemberPlayerInfo($row, $connect)
	{
		$cPlayer = array();
		
		if ($this->captainsEnabled)
		{
			$this->loadCaptainArray($row);
		}
    
		for ($i = 1; $i <= MAXPLAYERS; $i++)
		{
			$playerCodeVariable = getPlayerCodeVariable($i);
			$playerWeekInVariable = getPlayerWeekInVariable($i);
			$playerWeekOutVariable = getPlayerWeekOutVariable($i);

			if ($row[$playerCodeVariable] != NULL)		// Is this a valid player
			{
				$cPlayer[$i] = new player($row[$playerCodeVariable]);
				$error |= $cPlayer[$i]->setPlayerWeekIn($row[$playerWeekInVariable]);
				$error |= $cPlayer[$i]->setPlayerWeekOut($row[$playerWeekOutVariable]);
				$cPlayer[$i]->calculatePlayerScore($connect);
				$this->setScoresForEachWeek($cPlayer[$i]);
			}
		}
	}
	function getMemberTotalScore()
	{
		for ($i = 1; $i <= $this->maxWeeks; $i++)
		{
			$temp += $this->scorePerWeekArray[$i];
		}
		return $temp;
	}
	
	function setMatchingScore($temp, $weekString)
	{
		$this->matchingScorePerWeekArray[$weekString] = $temp;
	}
	function getMatchingScore($weekString)
	{
		return $this->matchingScorePerWeekArray[$weekString];
	}
	
	function setPosition($position, $weekString)
	{
		$this->positionPerWeekArray[$weekString] = $position;
	}
	function getPosition($weekString)
	{
		return $this->positionPerWeekArray[$weekString];
	}
	
	function addFormation($position)
	{
		$error = 0;
		switch($position)
		{
			case "GK": 	$this->numGK++;
						break;
			case "DEF":	$this->numDEF++;
						break;
			case "MID": $this->numMID++;
						break;
			case "ATT":	$this->numATT++;
						break;
			default:	//echo"<BR>Error detected for player code ".$memberPlayer[$i]->getCode().". Position not recognised.";
						$error = 1;
						break;
		}
		return $error;
	}
	function removeFromFormation($position)
	{
		$error = 0;
		switch($position)
		{
			case 'GK'	:	$this->numGK--;
							break;
			case 'DEF'	:	$this->numDEF--;
							break;
			case 'MID'	:	$this->numMID--;
							break;
			case 'ATT'	:	$this->numATT--;
							break;
			default		:	//echo"<BR>Error detected for player code ".$memberPlayer[$i]->getCode().". Position not recognised.";
							$error = 1;
							break;
		}
		return $error;
	}
	function getCurrentFormation()
	{
		return $this->numGK."-".$this->numDEF."-".$this->numMID."-".$this->numATT;
	}
	function checkFormation()
	{
		// Check the 4 possible formations
		// Check for 3-5-2
		$pass = 0;
		if (($this->numGK == 1) && ($this->numDEF == 3) && ($this->numMID == 5) && ($this->numATT == 2))
		{
			$pass |= 1;
		}
		// Check for 4-4-2
		if (($this->numGK == 1) && ($this->numDEF == 4) && ($this->numMID == 4) && ($this->numATT == 2))
		{
			$pass |= 1;
		}
		// Check for 4-3-3
		if (($this->numGK == 1) && ($this->numDEF == 4) && ($this->numMID == 3) && ($this->numATT == 3))
		{
			$pass |= 1;
		}
		// Check for 4-5-1
		if (($this->numGK == 1) && ($this->numDEF == 4) && ($this->numMID == 5) && ($this->numATT == 1))
		{
			$pass |= 1;
		}
		// Check for 3-4-3
		if (($this->numGK == 1) && ($this->numDEF == 3) && ($this->numMID == 4) && ($this->numATT == 3))
		{
			$pass |= 1;
		}
		
		$error = !$pass;
		
		if ($error)
			echo"<BR>Incorrect formation ".$this->numGK."-".$this->numDEF."-".$this->numMID."-".$this->numATT;
		return $error;
	}
	function setScoresForEachWeek($cPlayer)
	{
		//echo "<BR>player ".$cPlayer->getPlayerCode();
		for ($i = 1; $i <= $this->maxWeeks; $i++)
		{
			if ($this->captainsEnabled == 0)
			{
				$this->scorePerWeekArray[$i] += $cPlayer->getPlayerScoreForWeek($i);
			}
			else
			{
				// Check to see if this player is a captain for this week
				
				if ($this->captain[$i] == $cPlayer->getPlayerCode())
				{
					$this->scorePerWeekArray[$i] += ($cPlayer->getPlayerScoreForWeek($i) * 2);
					//echo "<BR>week". $i." captain ".$this->captain[$i];
				}
				else
				{
					$this->scorePerWeekArray[$i] += $cPlayer->getPlayerScoreForWeek($i);
				}
				//$this->scorePerWeekArray[$i] += $cPlayer->getPlayerScoreForWeekCaptains($i, $this->captain);
			}
		}
	}
	function getScoreForWeek($week)
	{
		return $this->scorePerWeekArray[$week];
	}
	function getScoreAtWeek($week)
	{
		$accumScore = 0;
		
		for ($i = 1; $i <= $week; $i++)
		{
			$accumScore += $this->scorePerWeekArray[$i];
		}
		return $accumScore;
	}
}

?>